package com.chuanjiabao.wisdom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.FamilyDeatilCallbackBean;
import com.chuanjiabao.wisdom.bean.ReasionInfoCallbackBean;
import com.chuanjiabao.wisdom.databinding.ItemIndexFamilyUsersBinding;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;
import com.chuanjiabao.wisdom.util.GlideUtil;
import com.chuanjiabao.wisdom.util.LogUtil;
import com.chuanjiabao.wisdom.util.SpUtils;

import java.util.List;

/**
 * Created by 86178 on 2019/7/24.
 * author mogen
 * 用心，成就一切。
 */

public class IndexFamilyUsersAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<FamilyDeatilCallbackBean.DataBean.MembersBean> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;
    ReasionInfoCallbackBean.DataBean bean;

    public IndexFamilyUsersAdapter(Context context, List<FamilyDeatilCallbackBean.DataBean.MembersBean> strings) {
        this.mcontext = context;
        this.strings = strings;
        bean = (ReasionInfoCallbackBean.DataBean) SpUtils.getBean("module_reasion");
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemIndexFamilyUsersBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_index_family_users, parent, false);
        binding.setAdapter(this);
        return new TourViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;
        if (!strings.get(position).getD_head_pic().startsWith("http")) {
            //没有片，显示文字
            tv.ivModuleHead.setVisibility(View.GONE);
            tv.tvModuleNameFirst.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(strings.get(position).getD_name())) {
                tv.tvModuleNameFirst.setVisibility(View.GONE);
                tv.ivModuleHead.setVisibility(View.VISIBLE);
                tv.tvModuleNameFirst.setText(strings.get(position).getD_relation().substring(0, 1));
            }else{
                tv.tvModuleNameFirst.setText(strings.get(position).getD_name().substring(0, 1));
            }
        } else {
            //有图片，显示图片
            tv.tvModuleNameFirst.setVisibility(View.GONE);
            tv.ivModuleHead.setVisibility(View.VISIBLE);
            GlideUtil.setCircleMethod(mcontext, strings.get(position).getD_head_pic(), tv.ivModuleHead);
        }

        try {
            if (strings.get(position).getD_user_id() == bean.getId()) {
                tv.llModuleAdmin.setBackgroundResource(R.drawable.module_draw_index_family_list_myself);
            }
        } catch (Exception e) {
            LogUtil.e("个人信息获取有误");
        }

        switch (strings.get(position).getD_is_admin()) {
            case 1:
                tv.llModuleAdmin.setBackgroundResource(R.drawable.module_draw_index_family_list_admin);
                tv.vModule.setBackgroundColor(mcontext.getResources().getColor(R.color.white));
                tv.tvModuleName.setTextColor(mcontext.getResources().getColor(R.color.white));
                break;
            default:
        }

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<FamilyDeatilCallbackBean.DataBean.MembersBean> {
        ItemIndexFamilyUsersBinding binding;
        TextView tvModuleNameFirst, tvModuleName;
        ImageView ivModuleHead;
        LinearLayout llModuleAdmin;
        View vModule;

        public TourViewHolder(ItemIndexFamilyUsersBinding dataBinding) {
            super(dataBinding.getRoot());
            binding = dataBinding;
            tvModuleNameFirst = dataBinding.tvModuleNameFirst;
            ivModuleHead = dataBinding.ivModuleHead;
            vModule = dataBinding.vModule;
            tvModuleName = dataBinding.tvModuleName;
            llModuleAdmin = dataBinding.llModueAdmin;
        }

        @Override
        public void bindData(FamilyDeatilCallbackBean.DataBean.MembersBean data) {
            binding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }
}